using GEM.Application.Common;
using GEM.Application.Dto;
using GEM.Application.Dto.User;
using GEM.Application.Repositories;
using GEM.Domain.Entities.App;
using GEM.Domain.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GEM.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;
    //依赖注入
    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    //查询所有用户的基本信息
    // [HttpGet("info")]
    // public async Task<IActionResult> GetUserInfo()
    // {
    //     var users = await _userService.GetUserListAsync();
    //     return Ok(ApiResponse<IEnumerable<UserDto>>.Success(users));
    // }

    //分页查询用户
    [HttpGet("paged")]
    public async Task<IActionResult> GetUserPaged([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20)
    {
        var pagedResult = await _userService.GetUserPagedAsync(pageIndex, pageSize);
        return Ok(ApiResponse<PagedResult<UserDto>>.Success(pagedResult));
    }

    //根据id查询用户的基本信息
    [HttpGet("byidinfo/{id}")]
    public async Task<IActionResult> GetByIdUserInfot(Guid id)
    {
        var user = await _userService.GetByIdAsync(id);
        return Ok(user);
    }

    // 软删除
    [HttpDelete("{id}")]
    public async Task<IActionResult> Deleted(Guid id)
    {
        var isTrue = await _userService.DeletedByIdAsync(id);
        return Ok(isTrue);
    }

    //编辑用户
    [HttpPut("dto/{id}")]
    public async Task<IActionResult> Update(Guid id, UserDto userDto)
    {

        var number = await _userService.UserUpdateAsync(id, userDto);
        var msg = number > 0 ? "更新成功" : "更新失败";
        if (number > 0)
        {
            return Ok(ApiResponse<string>.Success(msg));
        }
        return Ok(ApiResponse<string>.Fail(1004, msg));
    }

    //启用用户
    [HttpGet("enable/{id}")]
    public async Task<IActionResult> Enable(Guid id)
    {
        var isTrue = await _userService.EnableUserAsync(id);
        return Ok(isTrue);
    }

    //禁用用户
    [HttpGet("disable/{id}")]
    public async Task<IActionResult> Disable(Guid id)
    {
        var isTrue = await _userService.DisableUserAsync(id);
        return Ok(isTrue);
    }

    //修改密码
    [HttpPost("changepwd/{id}")]
    public async Task<IActionResult> ChangePwd(Guid id, [FromBody] ChangeUserPwdDto changeUserPwdDto)
    {
        var isTrue = await _userService.ChangePasswordAsync(id, changeUserPwdDto.OldPwd, changeUserPwdDto.NewPwd);
        return Ok(isTrue);
    }

    //为指定用户分配指定角色
    [HttpPost("assignrole")]
    public async Task<IActionResult> AssignRole([FromBody] AssignRoleDto assignRoleDto)
    {
        var result = await _userService.AssignRoleAsync(assignRoleDto.UserId, assignRoleDto.RoleId);
        return Ok(result);
    }

    //为指定用户移除指定角色
    [HttpPost("removerole")]
    public async Task<IActionResult> RemoveRole([FromBody] AssignRoleDto assignRoleDto)
    {
        var result = await _userService.RemoveRoleAsync(assignRoleDto.UserId, assignRoleDto.RoleId);
        return Ok(result);
    }

    //为指定用户修改昵称
    [HttpPost("updatenickname")]
    public async Task<IActionResult> UpdateNickname([FromBody] ChangeNicknameDto changeNicknameDto)
    {
        var result = await _userService.ChangeNicknameAsync(changeNicknameDto.UserId, changeNicknameDto.Nickname);
        return Ok(result);
    }

    //为指定用户修改头像
    [HttpPost("changeavatarurl")]
    public async Task<IActionResult> ChangeAvatarUrl([FromBody] ChangeAvartarUrlDto changeAvartarUrlDto)
    {
        var result = await _userService.ChangeAvatarUrlAsync(changeAvartarUrlDto.UserId, changeAvartarUrlDto.AvatarUrl);
        return Ok(result);
    }

    //为指定用户修改邮箱
    [HttpPost("changeemail")]
    public async Task<IActionResult> ChangeEmail([FromBody] ChangeEmailDto changeEmailDto)
    {
        var result = await _userService.ChangeEmailAsync(changeEmailDto.UserId, changeEmailDto.Email);
        return Ok(result);
    }

    //修改指定用户的所有基本信息
    [HttpPost("changeuser/{id}")]
    public async Task<IActionResult> ChangeUser(Guid id, [FromBody] ChangeUserDto changeUserDto)
    {
        var result = await _userService.ChangeUserAsync(id, changeUserDto);
        return Ok(result);
    }

}